package com.wuercloud.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.net.InetSocketAddress;

/**
 * Created by JiangWL on 2017/2/24.
 */
@Configuration
@PropertySource(value = "classpath:http/httpConf.properties",ignoreResourceNotFound = true)
public class HttpConfig {

    @Value("${boss.thread.count}")
    private int bossCount;

    @Value("${worker.thread.count}")
    private int workerCount;

    @Value("${http.server.port}")
    private int httpServerPort;



    @Bean(name = "bossGroup", destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup bossGroup() {
        return new NioEventLoopGroup(bossCount);
    }

    @Bean(name = "workerGroup", destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup workerGroup() {
        return new NioEventLoopGroup(workerCount);
    }

    @Bean(name = "httpAddress")
    public InetSocketAddress socketAddressPort() {
        return new InetSocketAddress(httpServerPort);
    }

    @SuppressWarnings("unchecked")
    @Bean(name = "serverBootstrap")
    public ServerBootstrap bootstrap() {
        ServerBootstrap b = new ServerBootstrap();
        b.group(bossGroup(), workerGroup())
                .channel(NioServerSocketChannel.class);

        return b;
    }
/*
    @Bean
    public HttpJsonServer httpJsonServer(){
        System.out.println(environment.getProperty("http.server")+" -- "+environment.getProperty("http.server.port"));
        return new HttpJsonServer(Integer.parseInt(environment.getProperty("http.server.port")));
    }*/
}
